/** @jsxImportSource @emotion/react */
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, OriginalViewProps, useItemInputControl,} from "../../common/base";
import {assertNamesUnique, createItem,} from "../../common/eHelper";
import {useRecordListSubr} from "../../hook/useRecordListSub";
import {DeviceSurvey} from "../../elevator/editor";
import {useMedia} from "use-media";
import {BlobInputList, Button, InputLine, Text, TextArea, useToast} from "customize-easy-ui-component";
import {EditStorageContext} from "../../StorageContext";
import {render设备类别} from "../../common/render";

//后端有提供  制造单位；可以当作默认取值吗？不行会被覆盖；#只能另外取一个名字。
export const input制造单位 = {
  edit:(inp:any,setInp:(a:any)=>void)=><InputLine label='制造单位'>
            <BlobInputList value={inp?.制单位??''} rows={1} datalist={[inp?.制造单位]}
                          onListChange={v => setInp({...inp, 制单位: v || undefined}) } />
        </InputLine>,
  names: ['制造单位','制单位'],
    // toTail: '设计温度太多了需要另行设定',
};

export const config设备概况 = [
  [['制造单位', '制单位', input制造单位] ],
  [['制造许可级别', '制许级',],  ['制造许可证编号', '制许可号' ] ],
  [['设备类别', '_$设备类别',render设备类别], ['产品名称','产品名'] ],
  [['产品批号', '产批号'], ['设备代码', '设代码']],
  [['设计单位', '设单位'], ],
  [['设计许可证编号', '设许可号',],  ['产品图号', '产图号' ] ],
  [['设计日期', {n:'设计日',t:'d'} ], ['制造日期', {n:'制造日',t:'d'} ], ],
  [['本证书适用的产品编号', {n:'适用编号',t:'B'}] ],
  [['监检所抽的产品编号', {n:'抽编号',t:'m'}] ],
];

//报错Cannot access 'EntranceSetup' before initialization 就因为import {display额定是, input额定是} from "../external/orcBase";
export const EntranceSetup =
    React.forwardRef((
        {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
    ) => {
      const atPrint = useMedia('print');
      const toast = useToast();
      const doCheckNames = React.useCallback((verId: string) => {
        const result = assertNamesUnique([{value: rep?.tzFields},
          {value: config设备概况, type:'esnt'},
          {value:['Projects', ]}
        ]);
        toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
      }, [rep,toast]);
      const {storage, setStorage} =React.useContext(EditStorageContext) as any;
      const getInpFilter = React.useCallback((par: any) => {
        const {_tblFixed, } =par||{};
        return {_tblFixed, };
      }, []);
      const {inp, setInp} = useItemInputControl({ref});
      if (atPrint) return null;
      else return (
          <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                               alone={alone} label={'初始化本报告，默认值配置等'}>
            {process.env.REACT_APP_TEST==='true' && <><div>
              <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
              <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
              <Text variant="h5">设置待测试表格的各列宽度：</Text>
              <TextArea value={storage?._tblFixed || ''} rows={2} onChange={e =>{
                JSON.parse(e.currentTarget.value??'[]');
                setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}); }}/>
            </div></>
            }
            <hr/>
          </InspectRecordLayout>
      );
    });



const recordPrintList =[
  createItem('Entrance', <EntranceSetup/>),
  createItem('Survey', <DeviceSurvey config={config设备概况} label={'特种设备制造监督检验证书'}
                        desc='（批量制造压力容器）没有设备台账，需要详细录入制造的设备明细数据'
            />),
];

export const OriginalView=
  React.forwardRef((
    { action: modAction,  verId, repId='', rep}
    :OriginalViewProps, ref
  ) => {
    const {list}=useRecordListSubr(ref,rep,recordPrintList,modAction,verId);
    return <React.Fragment>
      {list}
    </React.Fragment>;
  } );
